using System;
using System.Xml;
using UnityEngine;

namespace EventBehaviorTree
{
    public static class BlackboardHelper
    {
        public static void Reg_BlackboardKV(string fileName, string path, Blackboard blackboard)
        {
            XmlNodeList xnList = XmlHelper.GetXmlNodeList(fileName, path);
            foreach (XmlNode xn in xnList)
            {
                switch (xn.Attributes["Type"].Value)
                {
                    case "System.Boolean":
                        blackboard.Reg_Data(xn.Attributes["Key"].Value,
                            xn.InnerText == "" ? new BB_bool() : new BB_bool(bool.Parse(xn.InnerText)));
                        break;
                    case "System.Int32":
                        blackboard.Reg_Data(xn.Attributes["Key"].Value,
                            xn.InnerText == "" ? new BB_int() : new BB_int(int.Parse(xn.InnerText)));
                        break;
                    case "System.Single":
                        blackboard.Reg_Data(xn.Attributes["Key"].Value,
                            xn.InnerText == "" ? new BB_float() : new BB_float(float.Parse(xn.InnerText)));
                        break;
                    default:
                        break;
                }
            }
        }

        public static bool Compare(BBDataBase lhs, BBDataBase rhs, CmprOP cmprOP)
        {
            switch (lhs)
            {
                case BB_bool ebt_Data:
                    switch (cmprOP)
                    {
                        case CmprOP.Equal:
                            return ebt_Data == (rhs as BB_bool);
                        case CmprOP.NotEqual:
                            return ebt_Data != (rhs as BB_bool);
                        default:
                            return false;
                    }
                case BB_int ebt_Data:
                    switch (cmprOP)
                    {
                        case CmprOP.Greater:
                            return ebt_Data > (rhs as BB_int);
                        case CmprOP.GreaterOrEqual:
                            return ebt_Data >= (rhs as BB_int);
                        case CmprOP.Smaller:
                            return ebt_Data < (rhs as BB_int);
                        case CmprOP.SmallerOrEqual:
                            return ebt_Data <= (rhs as BB_int);
                        case CmprOP.Equal:
                            return ebt_Data == (rhs as BB_int);
                        case CmprOP.NotEqual:
                            return ebt_Data != (rhs as BB_int);
                        default:
                            return false;
                    }
                case BB_float ebt_Data:
                    switch (cmprOP)
                    {
                        case CmprOP.Greater:
                            return ebt_Data > (rhs as BB_float);
                        case CmprOP.GreaterOrEqual:
                            return ebt_Data >= (rhs as BB_float);
                        case CmprOP.Smaller:
                            return ebt_Data < (rhs as BB_float);
                        case CmprOP.SmallerOrEqual:
                            return ebt_Data <= (rhs as BB_float);
                        case CmprOP.Equal:
                            return ebt_Data == (rhs as BB_float);
                        case CmprOP.NotEqual:
                            return ebt_Data != (rhs as BB_float);
                        default:
                            return false;
                    }
                default:
                    return false;
            }
        }
        public static bool Compare(BBDataBase ebt_bool, CmprOP cmprOP)
        {
            if (ebt_bool is BB_bool)
            {
                switch (cmprOP)
                {
                    case CmprOP.Equal:
                        return (ebt_bool as BB_bool).GetValue() == true;
                    case CmprOP.NotEqual:
                        return (ebt_bool as BB_bool).GetValue() != true;
                    default:
                        return false;
                }
            }
            return false;
        }
    }
}
